package com.sx.pipeline.ui.supply.approve

import android.app.Application
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.AuditTaskDTO
import com.sx.pipeline.repos.http.res.Ipage
import com.sx.pipeline.repos.http.res.ReleaseSupplyVo
import com.sx.pipeline.repos.http.res.SupplyDetailVo
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description: TODO
 */
class ApprovalSupplyVM(app: Application): CYBaseViewModel(app) {

    /**
     * 获取我审批类型标题
     */
    fun getApprovalSupplyTabsList(): List<String> {
        val tabs = mutableListOf<String>()
        tabs.add(getApplication<Application>().getString(R.string.txt_pending_review))
        tabs.add(getApplication<Application>().getString(R.string.txt_passed))
        tabs.add(getApplication<Application>().getString(R.string.txt_rejected))
        return tabs
    }


    fun getApprovalSupplyList(): MutableList<ReleaseTaskListVO> {
        val releaseList = mutableListOf<ReleaseTaskListVO>()

        val device1 = ReleaseTaskListVO("1",1)
        val device2 = ReleaseTaskListVO("2",2)
        val device3 = ReleaseTaskListVO("3",3)

        releaseList.add(device1)
        releaseList.add(device2)
        releaseList.add(device3)


        return releaseList
    }


    /**
     * 获取审批供应列表
     */
    var page=1
    val approvalSupplyListEvent: SingleLiveEvent<List<ReleaseSupplyVo>> = SingleLiveEvent()
    fun auditSupplyList(status: Int) {
        addSubscribe(
            mModel.auditSupplyList(status,page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<ReleaseSupplyVo>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Ipage<ReleaseSupplyVo>) {
                        approvalSupplyListEvent.postValue(result.records)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 审核任务
     * @param id 任务id
     * @param remark 驳回原因
     * @param status 1审核通过  2审核驳回
     */
    val auditSuccessEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    fun getSupplyAudit(id: Int,remark: String,status:Int) {
        val dto= AuditTaskDTO(id,remark,status)
        addSubscribe(
            mModel.getSupplyAudit(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        auditSuccessEvent.postValue(true)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 获取供应详情
     */
    val supplyDetailEvent: SingleLiveEvent<SupplyDetailVo> = SingleLiveEvent()
    fun getSupplyOrderDetail(id: Int) {
        addSubscribe(
            mModel.getSupplyOrderDetail(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<SupplyDetailVo, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: SupplyDetailVo) {
                        supplyDetailEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        dismissLoadingDialog()
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}